package com.gtr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gtr.entity.OrderMaster;
import com.gtr.form.BuyerOrderForm;
import com.gtr.service.OrderMasterService;
import com.gtr.util.ResultVOUtil;
import com.gtr.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单详情表 前端控制器
 * </p>
 *
 * @author gtr
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/buyer/order")
public class BuyerOrderController {
    @Autowired
    private OrderMasterService orderMasterService;

    @PostMapping("/create") //创建订单
    public ResultVO create(@RequestBody BuyerOrderForm buyerOrderForm) {
       String orderId = this.orderMasterService.create(buyerOrderForm);
//       ResultVO resultVO = new ResultVO();
//       resultVO.setCode(0);
//       resultVO.setMsg("成功");
       Map<String, String> map = new HashMap<>();
       map.put("orderId", orderId);
       return ResultVOUtil.success(map);
    }

    @GetMapping("/list/{buyerId}/{page}/{size}")
    public ResultVO list( //某一用户的预约订单列表
            @PathVariable("buyerId") Integer buyerId,
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size
            ) {
//        Page<OrderMaster> orderMasterPage = new Page<>(page, size);
//        Page<OrderMaster> resultPage = this.orderMasterService.page(orderMasterPage); //分页结果
//        List<OrderMaster> records = resultPage.getRecords(); //分页结果的数据
//        return ResultVOUtil.success(records);
        Page<OrderMaster> orderMasterPage = new Page<>(page,size);
        QueryWrapper<OrderMaster> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("buyer_openid", buyerId);
        Page<OrderMaster> resultPage = this.orderMasterService.page(orderMasterPage,queryWrapper);
        List<OrderMaster> records = resultPage.getRecords();
        return ResultVOUtil.success(records);
    }

    @GetMapping("/detail/{buyerId}/{orderId}")
    public ResultVO detail( //预约订单详情
            @PathVariable("buyerId") Integer buyerId,
            @PathVariable("orderId") String orderId
    ){
        return ResultVOUtil.success(this.orderMasterService.detail(buyerId, orderId));
    }

    @PutMapping("/cancel/{buyerId}/{orderId}")
    public ResultVO cancel( //取消预约
            @PathVariable("buyerId") Integer buyerId,
            @PathVariable("orderId") String orderId
    ){
        this.orderMasterService.cancel(buyerId, orderId);
        return ResultVOUtil.success(null);
    }

    @PutMapping("/finish/{orderId}")
    public ResultVO finish( //完成预约
            @PathVariable("orderId") String orderId
    ){
        this.orderMasterService.finish(orderId);
        return ResultVOUtil.success(null);
    }

    @PutMapping("/pay/{buyerId}/{orderId}")
    public ResultVO pay( //支付订单
            @PathVariable("buyerId") Integer buyerId,
            @PathVariable("orderId") String orderId
    ){
        this.orderMasterService.pay(buyerId, orderId);
        return ResultVOUtil.success(null);
    }
}

